package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcPatrolConfig;
import com.csun.cmny.provider.model.dto.patrol.PatroTaskPageDto;
import com.csun.cmny.provider.model.dto.patrol.PatrolTaskDto;
import com.csun.cmny.provider.model.dto.patrol.PatrolTaskIdDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcPatrolConfigService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping(value = "/patrolTask", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcPatrolConfigController extends BaseController {

    @Resource
    private DmcPatrolConfigService dmcPatrolConfigServiceimp;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    /*
    * 新增巡更任务
    * */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Wrapper patrolTaskAdd(@RequestBody PatrolTaskDto patrolTaskDto){
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        patrolTaskDto.setGroupId(loginAuthDto.getGroupId());
        DmcPatrolConfig dmcPatrolConfig = new DmcPatrolConfig();
        BeanUtils.copyProperties(patrolTaskDto,dmcPatrolConfig);
        dmcPatrolConfig.setCreator(getLoginAuthDto().getUserName());
        dmcPatrolConfig.setCreatorId(getLoginAuthDto().getUserId());
        Integer result = dmcPatrolConfigServiceimp.save(dmcPatrolConfig);
        if (result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
    * 删除巡更任务
    * */
    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public Wrapper patrolTaskDelete(@RequestBody PatrolTaskIdDto patrolTaskIdDto){
        patrolTaskIdDto.setGroupId(getLoginAuthDto().getGroupId());
        Integer result = dmcPatrolConfigServiceimp.patrolTaskDelete(patrolTaskIdDto);
        if (result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
    * 修改巡更任务
    * */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Wrapper patrolTaskUpdate (@RequestBody PatrolTaskDto patrolTaskDto ){
        DmcPatrolConfig dmcPatrolConfig = new DmcPatrolConfig();
        BeanUtils.copyProperties(patrolTaskDto,dmcPatrolConfig);
        dmcPatrolConfig.setLastOperator(getLoginAuthDto().getUserName());
        dmcPatrolConfig.setLastOperatorId(getLoginAuthDto().getUserId());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        try{
            Date date = df.parse(format);
            dmcPatrolConfig.setUpdateTime(date);
        }catch (Exception e){}
        Integer result = dmcPatrolConfigServiceimp.patrolTaskUpdate(dmcPatrolConfig);
        if (result<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    /*
    * 查询所有巡更任务
    * */
    @RequestMapping(value = "/queryGroupPatrolTaskListWithPage",method = RequestMethod.POST)
    public Wrapper patrolTaskSelect(@RequestBody PatroTaskPageDto patroTaskPageDto){
        patroTaskPageDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = dmcPatrolConfigServiceimp.patrolTaskSelect(patroTaskPageDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
}
